package com.sunni.controller;

import com.sunni.entity.Classes;
import com.sunni.entity.Student;
import com.sunni.mapper.oneToMore.ClassesMapper;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class _03_02_一对多_集合级联查询 {
    public static void main(String[] args) {

// 1. 读取mybatis核心文件
        InputStream inputStream =
                ResolverUtil.Test.class.getClassLoader().getResourceAsStream("config.xml");

// 2.创建SqlSessionFactoryBuilder();
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

//  3.拿到sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

//  4.开启SqlSession对话
        SqlSession sqlSession = sqlSessionFactory.openSession();

// 5. SqlSession 获取 接口 get
        ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);

//  6.accountMapper ：mapper对象执行 mapper层方法
        Classes classes = classesMapper.findByCid(2);
        System.out.println(classes.getId()+"--"+classes.getName());
        for (Student student : classes.getStudentList()) {
            System.out.println(student);
        }

// 7.提交sql会话 （针对 增、删、改，能让数据库发生变化的使用commit）
        sqlSession.commit();

//  8.关闭sql会话
        sqlSession.close();
    }
}
